home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume21 / berkeley_yacc / part02 < prev    next >
Encoding:
Internet Message Format  |  1990-04-05  |  37.6 KB

  1. Subject:  v21i079:  Public domain Berkeley YACC, Part02/05
  2. Newsgroups: comp.sources.unix
  3. Approved: rsalz@uunet.UU.NET
  4. X-Checksum-Snefru: 086537c0 b0566e5c 37f6560f 2a08cfa4
  5.  
  6. Submitted-by: Robert Corbett <corbett@ernie.berkeley.edu>
  7. Posting-number: Volume 21, Issue 79
  8. Archive-name: berkeley_yacc/part02
  9.  
  10. #! /bin/sh
  11. # This is a shell archive.  Remove anything before this line, then unpack
  12. # it by saving it into a file and typing "sh file".  To overwrite existing
  13. # files, type "sh file -c".  You can also feed this as standard input via
  14. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  15. # will see the following message at the end:
  16. #        "End of archive 2 (of 5)."
  17. # Contents:  lalr.c lr0.c mkpar.c skeleton.c
  18. # Wrapped by rsalz@litchi.bbn.com on Mon Apr  2 11:43:42 1990
  19. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  20. if test -f 'lalr.c' -a "${1}" != "-c" ; then 
  21.   echo shar: Will not clobber existing file \"'lalr.c'\"
  22. else
  23. echo shar: Extracting \"'lalr.c'\" \(10213 characters\)
  24. sed "s/^X//" >'lalr.c' <<'END_OF_FILE'
  25. X#include "defs.h"
  26. X
  27. Xtypedef
  28. X  struct shorts
  29. X    {
  30. X      struct shorts *next;
  31. X      short value;
  32. X    }
  33. X  shorts;
  34. X
  35. Xint tokensetsize;
  36. Xshort *lookaheads;
  37. Xshort *LAruleno;
  38. Xunsigned *LA;
  39. Xshort *accessing_symbol;
  40. Xcore **state_table;
  41. Xshifts **shift_table;
  42. Xreductions **reduction_table;
  43. Xshort *goto_map;
  44. Xshort *from_state;
  45. Xshort *to_state;
  46. X
  47. Xshort **transpose();
  48. X
  49. Xstatic int infinity;
  50. Xstatic int maxrhs;
  51. Xstatic int ngotos;
  52. Xstatic unsigned *F;
  53. Xstatic short **includes;
  54. Xstatic shorts **lookback;
  55. Xstatic short **R;
  56. Xstatic short *INDEX;
  57. Xstatic short *VERTICES;
  58. Xstatic int top;
  59. X
  60. X
  61. Xlalr()
  62. X{
  63. X    tokensetsize = WORDSIZE(ntokens);
  64. X
  65. X    set_state_table();
  66. X    set_accessing_symbol();
  67. X    set_shift_table();
  68. X    set_reduction_table();
  69. X    set_maxrhs();
  70. X    initialize_LA();
  71. X    set_goto_map();
  72. X    initialize_F();
  73. X    build_relations();
  74. X    compute_FOLLOWS();
  75. X    compute_lookaheads();
  76. X}
  77. X
  78. X
  79. X
  80. Xset_state_table()
  81. X{
  82. X    register core *sp;
  83. X
  84. X    state_table = NEW2(nstates, core *);
  85. X    for (sp = first_state; sp; sp = sp->next)
  86. X    state_table[sp->number] = sp;
  87. X}
  88. X
  89. X
  90. X
  91. Xset_accessing_symbol()
  92. X{
  93. X    register core *sp;
  94. X
  95. X    accessing_symbol = NEW2(nstates, short);
  96. X    for (sp = first_state; sp; sp = sp->next)
  97. X    accessing_symbol[sp->number] = sp->accessing_symbol;
  98. X}
  99. X
  100. X
  101. X
  102. Xset_shift_table()
  103. X{
  104. X    register shifts *sp;
  105. X
  106. X    shift_table = NEW2(nstates, shifts *);
  107. X    for (sp = first_shift; sp; sp = sp->next)
  108. X    shift_table[sp->number] = sp;
  109. X}
  110. X
  111. X
  112. X
  113. Xset_reduction_table()
  114. X{
  115. X    register reductions *rp;
  116. X
  117. X    reduction_table = NEW2(nstates, reductions *);
  118. X    for (rp = first_reduction; rp; rp = rp->next)
  119. X    reduction_table[rp->number] = rp;
  120. X}
  121. X
  122. X
  123. X
  124. Xset_maxrhs()
  125. X{
  126. X  register short *itemp;
  127. X  register short *item_end;
  128. X  register int length;
  129. X  register int max;
  130. X
  131. X  length = 0;
  132. X  max = 0;
  133. X  item_end = ritem + nitems;
  134. X  for (itemp = ritem; itemp < item_end; itemp++)
  135. X    {
  136. X      if (*itemp >= 0)
  137. X    {
  138. X      length++;
  139. X    }
  140. X      else
  141. X    {
  142. X      if (length > max) max = length;
  143. X      length = 0;
  144. X    }
  145. X    }
  146. X
  147. X  maxrhs = max;
  148. X}
  149. X
  150. X
  151. X
  152. Xinitialize_LA()
  153. X{
  154. X  register int i, j, k;
  155. X  register reductions *rp;
  156. X
  157. X  lookaheads = NEW2(nstates + 1, short);
  158. X
  159. X  k = 0;
  160. X  for (i = 0; i < nstates; i++)
  161. X    {
  162. X      lookaheads[i] = k;
  163. X      rp = reduction_table[i];
  164. X      if (rp)
  165. X    k += rp->nreds;
  166. X    }
  167. X  lookaheads[nstates] = k;
  168. X
  169. X  LA = NEW2(k * tokensetsize, unsigned);
  170. X  LAruleno = NEW2(k, short);
  171. X  lookback = NEW2(k, shorts *);
  172. X
  173. X  k = 0;
  174. X  for (i = 0; i < nstates; i++)
  175. X    {
  176. X      rp = reduction_table[i];
  177. X      if (rp)
  178. X    {
  179. X      for (j = 0; j < rp->nreds; j++)
  180. X        {
  181. X          LAruleno[k] = rp->rules[j];
  182. X          k++;
  183. X        }
  184. X    }
  185. X    }
  186. X}
  187. X
  188. X
  189. Xset_goto_map()
  190. X{
  191. X  register shifts *sp;
  192. X  register int i;
  193. X  register int symbol;
  194. X  register int k;
  195. X  register short *temp_map;
  196. X  register int state2;
  197. X  register int state1;
  198. X
  199. X  goto_map = NEW2(nvars + 1, short) - ntokens;
  200. X  temp_map = NEW2(nvars + 1, short) - ntokens;
  201. X
  202. X  ngotos = 0;
  203. X  for (sp = first_shift; sp; sp = sp->next)
  204. X    {
  205. X      for (i = sp->nshifts - 1; i >= 0; i--)
  206. X    {
  207. X      symbol = accessing_symbol[sp->shift[i]];
  208. X
  209. X      if (ISTOKEN(symbol)) break;
  210. X
  211. X      if (ngotos == MAXSHORT)
  212. X        fatal("too many gotos");
  213. X
  214. X      ngotos++;
  215. X      goto_map[symbol]++;
  216. X        }
  217. X    }
  218. X
  219. X  k = 0;
  220. X  for (i = ntokens; i < nsyms; i++)
  221. X    {
  222. X      temp_map[i] = k;
  223. X      k += goto_map[i];
  224. X    }
  225. X
  226. X  for (i = ntokens; i < nsyms; i++)
  227. X    goto_map[i] = temp_map[i];
  228. X
  229. X  goto_map[nsyms] = ngotos;
  230. X  temp_map[nsyms] = ngotos;
  231. X
  232. X  from_state = NEW2(ngotos, short);
  233. X  to_state = NEW2(ngotos, short);
  234. X
  235. X  for (sp = first_shift; sp; sp = sp->next)
  236. X    {
  237. X      state1 = sp->number;
  238. X      for (i = sp->nshifts - 1; i >= 0; i--)
  239. X    {
  240. X      state2 = sp->shift[i];
  241. X      symbol = accessing_symbol[state2];
  242. X
  243. X      if (ISTOKEN(symbol)) break;
  244. X
  245. X      k = temp_map[symbol]++;
  246. X      from_state[k] = state1;
  247. X      to_state[k] = state2;
  248. X    }
  249. X    }
  250. X
  251. X  FREE(temp_map + ntokens);
  252. X}
  253. X
  254. X
  255. X
  256. X/*  Map_goto maps a state/symbol pair into its numeric representation.    */
  257. X
  258. Xint
  259. Xmap_goto(state, symbol)
  260. Xint state;
  261. Xint symbol;
  262. X{
  263. X    register int high;
  264. X    register int low;
  265. X    register int middle;
  266. X    register int s;
  267. X
  268. X    low = goto_map[symbol];
  269. X    high = goto_map[symbol + 1];
  270. X
  271. X    for (;;)
  272. X    {
  273. X    assert(low <= high);
  274. X    middle = (low + high) >> 1;
  275. X    s = from_state[middle];
  276. X    if (s == state)
  277. X        return (middle);
  278. X    else if (s < state)
  279. X        low = middle + 1;
  280. X    else
  281. X        high = middle - 1;
  282. X    }
  283. X}
  284. X
  285. X
  286. X
  287. Xinitialize_F()
  288. X{
  289. X  register int i;
  290. X  register int j;
  291. X  register int k;
  292. X  register shifts *sp;
  293. X  register short *edge;
  294. X  register unsigned *rowp;
  295. X  register short *rp;
  296. X  register short **reads;
  297. X  register int nedges;
  298. X  register int stateno;
  299. X  register int symbol;
  300. X  register int nwords;
  301. X
  302. X  nwords = ngotos * tokensetsize;
  303. X  F = NEW2(nwords, unsigned);
  304. X
  305. X  reads = NEW2(ngotos, short *);
  306. X  edge = NEW2(ngotos + 1, short);
  307. X  nedges = 0;
  308. X
  309. X  rowp = F;
  310. X  for (i = 0; i < ngotos; i++)
  311. X    {
  312. X      stateno = to_state[i];
  313. X      sp = shift_table[stateno];
  314. X
  315. X      if (sp)
  316. X    {
  317. X      k = sp->nshifts;
  318. X
  319. X      for (j = 0; j < k; j++)
  320. X        {
  321. X          symbol = accessing_symbol[sp->shift[j]];
  322. X          if (ISVAR(symbol))
  323. X        break;
  324. X          SETBIT(rowp, symbol);
  325. X        }
  326. X
  327. X      for (; j < k; j++)
  328. X        {
  329. X          symbol = accessing_symbol[sp->shift[j]];
  330. X          if (nullable[symbol])
  331. X        edge[nedges++] = map_goto(stateno, symbol);
  332. X        }
  333. X    
  334. X      if (nedges)
  335. X        {
  336. X          reads[i] = rp = NEW2(nedges + 1, short);
  337. X
  338. X          for (j = 0; j < nedges; j++)
  339. X        rp[j] = edge[j];
  340. X
  341. X          rp[nedges] = -1;
  342. X          nedges = 0;
  343. X        }
  344. X    }
  345. X
  346. X      rowp += tokensetsize;
  347. X    }
  348. X
  349. X  SETBIT(F, 0);
  350. X  digraph(reads);
  351. X
  352. X  for (i = 0; i < ngotos; i++)
  353. X    {
  354. X      if (reads[i])
  355. X    FREE(reads[i]);
  356. X    }
  357. X
  358. X  FREE(reads);
  359. X  FREE(edge);
  360. X}
  361. X
  362. X
  363. X
  364. Xbuild_relations()
  365. X{
  366. X  register int i;
  367. X  register int j;
  368. X  register int k;
  369. X  register short *rulep;
  370. X  register short *rp;
  371. X  register shifts *sp;
  372. X  register int length;
  373. X  register int nedges;
  374. X  register int done;
  375. X  register int state1;
  376. X  register int stateno;
  377. X  register int symbol1;
  378. X  register int symbol2;
  379. X  register short *shortp;
  380. X  register short *edge;
  381. X  register short *states;
  382. X  register short **new_includes;
  383. X
  384. X  includes = NEW2(ngotos, short *);
  385. X  edge = NEW2(ngotos + 1, short);
  386. X  states = NEW2(maxrhs + 1, short);
  387. X
  388. X  for (i = 0; i < ngotos; i++)
  389. X    {
  390. X      nedges = 0;
  391. X      state1 = from_state[i];
  392. X      symbol1 = accessing_symbol[to_state[i]];
  393. X
  394. X      for (rulep = derives[symbol1]; *rulep >= 0; rulep++)
  395. X    {
  396. X      length = 1;
  397. X      states[0] = state1;
  398. X      stateno = state1;
  399. X
  400. X      for (rp = ritem + rrhs[*rulep]; *rp >= 0; rp++)
  401. X        {
  402. X          symbol2 = *rp;
  403. X          sp = shift_table[stateno];
  404. X          k = sp->nshifts;
  405. X
  406. X          for (j = 0; j < k; j++)
  407. X        {
  408. X          stateno = sp->shift[j];
  409. X          if (accessing_symbol[stateno] == symbol2) break;
  410. X        }
  411. X
  412. X          states[length++] = stateno;
  413. X        }
  414. X
  415. X      add_lookback_edge(stateno, *rulep, i);
  416. X
  417. X      length--;
  418. X      done = 0;
  419. X      while (!done)
  420. X        {
  421. X          done = 1;
  422. X          rp--;
  423. X          if (ISVAR(*rp))
  424. X        {
  425. X          stateno = states[--length];
  426. X          edge[nedges++] = map_goto(stateno, *rp);
  427. X          if (nullable[*rp] && length > 0) done = 0;
  428. X        }
  429. X        }
  430. X    }
  431. X
  432. X      if (nedges)
  433. X    {
  434. X      includes[i] = shortp = NEW2(nedges + 1, short);
  435. X      for (j = 0; j < nedges; j++)
  436. X        shortp[j] = edge[j];
  437. X      shortp[nedges] = -1;
  438. X    }
  439. X    }
  440. X
  441. X  new_includes = transpose(includes, ngotos);
  442. X
  443. X  for (i = 0; i < ngotos; i++)
  444. X    if (includes[i])
  445. X      FREE(includes[i]);
  446. X
  447. X  FREE(includes);
  448. X
  449. X  includes = new_includes;
  450. X
  451. X  FREE(edge);
  452. X  FREE(states);
  453. X}
  454. X
  455. X
  456. Xadd_lookback_edge(stateno, ruleno, gotono)
  457. Xint stateno, ruleno, gotono;
  458. X{
  459. X    register int i, k;
  460. X    register int found;
  461. X    register shorts *sp;
  462. X
  463. X    i = lookaheads[stateno];
  464. X    k = lookaheads[stateno + 1];
  465. X    found = 0;
  466. X    while (!found && i < k)
  467. X    {
  468. X    if (LAruleno[i] == ruleno)
  469. X        found = 1;
  470. X    else
  471. X        ++i;
  472. X    }
  473. X    assert(found);
  474. X
  475. X    sp = NEW(shorts);
  476. X    sp->next = lookback[i];
  477. X    sp->value = gotono;
  478. X    lookback[i] = sp;
  479. X}
  480. X
  481. X
  482. X
  483. Xshort **
  484. Xtranspose(R, n)
  485. Xshort **R;
  486. Xint n;
  487. X{
  488. X  register short **new_R;
  489. X  register short **temp_R;
  490. X  register short *nedges;
  491. X  register short *sp;
  492. X  register int i;
  493. X  register int k;
  494. X
  495. X  nedges = NEW2(n, short);
  496. X
  497. X  for (i = 0; i < n; i++)
  498. X    {
  499. X      sp = R[i];
  500. X      if (sp)
  501. X    {
  502. X      while (*sp >= 0)
  503. X        nedges[*sp++]++;
  504. X    }
  505. X    }
  506. X
  507. X  new_R = NEW2(n, short *);
  508. X  temp_R = NEW2(n, short *);
  509. X
  510. X  for (i = 0; i < n; i++)
  511. X    {
  512. X      k = nedges[i];
  513. X      if (k > 0)
  514. X    {
  515. X      sp = NEW2(k + 1, short);
  516. X      new_R[i] = sp;
  517. X      temp_R[i] = sp;
  518. X      sp[k] = -1;
  519. X    }
  520. X    }
  521. X
  522. X  FREE(nedges);
  523. X
  524. X  for (i = 0; i < n; i++)
  525. X    {
  526. X      sp = R[i];
  527. X      if (sp)
  528. X    {
  529. X      while (*sp >= 0)
  530. X        *temp_R[*sp++]++ = i;
  531. X    }
  532. X    }
  533. X
  534. X  FREE(temp_R);
  535. X
  536. X  return (new_R);
  537. X}
  538. X
  539. X
  540. X
  541. Xcompute_FOLLOWS()
  542. X{
  543. X  digraph(includes);
  544. X}
  545. X
  546. X
  547. Xcompute_lookaheads()
  548. X{
  549. X  register int i, n;
  550. X  register unsigned *fp1, *fp2, *fp3;
  551. X  register shorts *sp, *next;
  552. X  register unsigned *rowp;
  553. X
  554. X  rowp = LA;
  555. X  n = lookaheads[nstates];
  556. X  for (i = 0; i < n; i++)
  557. X    {
  558. X      fp3 = rowp + tokensetsize;
  559. X      for (sp = lookback[i]; sp; sp = sp->next)
  560. X    {
  561. X      fp1 = rowp;
  562. X      fp2 = F + tokensetsize * sp->value;
  563. X      while (fp1 < fp3)
  564. X        *fp1++ |= *fp2++;
  565. X    }
  566. X      rowp = fp3;
  567. X    }
  568. X
  569. X  for (i = 0; i < n; i++)
  570. X    for (sp = lookback[i]; sp; sp = next)
  571. X      {
  572. X        next = sp->next;
  573. X        FREE(sp);
  574. X      }
  575. X
  576. X  FREE(lookback);
  577. X  FREE(F);
  578. X}
  579. X
  580. X
  581. Xdigraph(relation)
  582. Xshort **relation;
  583. X{
  584. X  register int i;
  585. X
  586. X  infinity = ngotos + 2;
  587. X  INDEX = NEW2(ngotos + 1, short);
  588. X  VERTICES = NEW2(ngotos + 1, short);
  589. X  top = 0;
  590. X
  591. X  R = relation;
  592. X
  593. X  for (i = 0; i < ngotos; i++)
  594. X    INDEX[i] = 0;
  595. X
  596. X  for (i = 0; i < ngotos; i++)
  597. X    {
  598. X      if (INDEX[i] == 0 && R[i])
  599. X    traverse(i);
  600. X    }
  601. X
  602. X  FREE(INDEX);
  603. X  FREE(VERTICES);
  604. X}
  605. X
  606. X
  607. X
  608. Xtraverse(i)
  609. Xregister int i;
  610. X{
  611. X  register unsigned *fp1;
  612. X  register unsigned *fp2;
  613. X  register unsigned *fp3;
  614. X  register int j;
  615. X  register short *rp;
  616. X
  617. X  int height;
  618. X  unsigned *base;
  619. X
  620. X  VERTICES[++top] = i;
  621. X  INDEX[i] = height = top;
  622. X
  623. X  base = F + i * tokensetsize;
  624. X  fp3 = base + tokensetsize;
  625. X
  626. X  rp = R[i];
  627. X  if (rp)
  628. X    {
  629. X      while ((j = *rp++) >= 0)
  630. X    {
  631. X      if (INDEX[j] == 0)
  632. X        traverse(j);
  633. X
  634. X      if (INDEX[i] > INDEX[j])
  635. X        INDEX[i] = INDEX[j];
  636. X
  637. X      fp1 = base;
  638. X      fp2 = F + j * tokensetsize;
  639. X
  640. X      while (fp1 < fp3)
  641. X        *fp1++ |= *fp2++;
  642. X    }
  643. X    }
  644. X
  645. X  if (INDEX[i] == height)
  646. X    {
  647. X      for (;;)
  648. X    {
  649. X      j = VERTICES[top--];
  650. X      INDEX[j] = infinity;
  651. X
  652. X      if (i == j)
  653. X        break;
  654. X
  655. X      fp1 = base;
  656. X      fp2 = F + j * tokensetsize;
  657. X
  658. X      while (fp1 < fp3)
  659. X        *fp2++ = *fp1++;
  660. X    }
  661. X    }
  662. X}
  663. END_OF_FILE
  664. if test 10213 -ne `wc -c <'lalr.c'`; then
  665.     echo shar: \"'lalr.c'\" unpacked with wrong size!
  666. fi
  667. # end of 'lalr.c'
  668. fi
  669. if test -f 'lr0.c' -a "${1}" != "-c" ; then 
  670.   echo shar: Will not clobber existing file \"'lr0.c'\"
  671. else
  672. echo shar: Extracting \"'lr0.c'\" \(9615 characters\)
  673. sed "s/^X//" >'lr0.c' <<'END_OF_FILE'
  674. X#include "defs.h"
  675. X
  676. Xextern short *itemset;
  677. Xextern short *itemsetend;
  678. Xextern unsigned *ruleset;
  679. X
  680. Xint nstates;
  681. Xcore *first_state;
  682. Xshifts *first_shift;
  683. Xreductions *first_reduction;
  684. X
  685. Xint get_state();
  686. Xcore *new_state();
  687. X
  688. Xstatic core *this_state;
  689. Xstatic core *last_state;
  690. Xstatic shifts *last_shift;
  691. Xstatic reductions *last_reduction;
  692. X
  693. Xstatic int nshifts;
  694. Xstatic short *shift_symbol;
  695. X
  696. Xstatic short *redset;
  697. Xstatic short *shiftset;
  698. X
  699. Xstatic short **kernel_base;
  700. Xstatic short **kernel_end;
  701. Xstatic short *kernel_items;
  702. X
  703. Xstatic core **state_table;
  704. X
  705. X
  706. Xallocate_itemsets()
  707. X{
  708. X  register short *itemp;
  709. X  register short *item_end;
  710. X  register int symbol;
  711. X  register int i;
  712. X  register int count;
  713. X  register int max;
  714. X  register short *symbol_count;
  715. X
  716. X  count = 0;
  717. X  symbol_count = NEW2(nsyms, short);
  718. X
  719. X  item_end = ritem + nitems;
  720. X  for (itemp = ritem; itemp < item_end; itemp++)
  721. X    {
  722. X      symbol = *itemp;
  723. X      if (symbol >= 0)
  724. X    {
  725. X      count++;
  726. X      symbol_count[symbol]++;
  727. X    }
  728. X    }
  729. X
  730. X  kernel_base = NEW2(nsyms, short *);
  731. X  kernel_items = NEW2(count, short);
  732. X
  733. X  count = 0;
  734. X  max = 0;
  735. X  for (i = 0; i < nsyms; i++)
  736. X    {
  737. X      kernel_base[i] = kernel_items + count;
  738. X      count += symbol_count[i];
  739. X      if (max < symbol_count[i])
  740. X    max = symbol_count[i];
  741. X    }
  742. X
  743. X  shift_symbol = symbol_count;
  744. X  kernel_end = NEW2(nsyms, short *);
  745. X}
  746. X
  747. X
  748. X
  749. Xallocate_storage()
  750. X{
  751. X  allocate_itemsets();
  752. X
  753. X  shiftset = NEW2(nsyms, short);
  754. X  redset = NEW2(nrules + 1, short);
  755. X  state_table = NEW2(nitems, core *);
  756. X}
  757. X
  758. X
  759. X
  760. Xappend_states()
  761. X{
  762. X  register int i;
  763. X  register int j;
  764. X  register int symbol;
  765. X
  766. X#ifdef    TRACE
  767. X  fprintf(stderr, "Entering append_states\n");
  768. X#endif
  769. X
  770. X  for (i = 1; i < nshifts; i++)
  771. X    {
  772. X      symbol = shift_symbol[i];
  773. X      j = i;
  774. X      while (j > 0 && shift_symbol[j - 1] > symbol)
  775. X    {
  776. X      shift_symbol[j] = shift_symbol[j - 1];
  777. X      j--;
  778. X    }
  779. X      shift_symbol[j] = symbol;
  780. X    }
  781. X
  782. X  for (i = 0; i < nshifts; i++)
  783. X    {
  784. X      symbol = shift_symbol[i];
  785. X      shiftset[i] = get_state(symbol);
  786. X    }
  787. X}
  788. X
  789. X
  790. Xfree_storage()
  791. X{
  792. X  FREE(shift_symbol);
  793. X  FREE(redset);
  794. X  FREE(shiftset);
  795. X  FREE(kernel_base);
  796. X  FREE(kernel_end);
  797. X  FREE(kernel_items);
  798. X  FREE(state_table);
  799. X}
  800. X
  801. X
  802. X
  803. Xgenerate_states()
  804. X{
  805. X  allocate_storage();
  806. X  itemset = NEW2(nitems, short);
  807. X  ruleset = NEW2(WORDSIZE(nrules), unsigned);
  808. X  set_first_derives();
  809. X  initialize_states();
  810. X
  811. X  while (this_state)
  812. X    {
  813. X      closure(this_state->items, this_state->nitems);
  814. X      save_reductions();
  815. X      new_itemsets();
  816. X      append_states();
  817. X
  818. X      if (nshifts > 0)
  819. X        save_shifts();
  820. X
  821. X      this_state = this_state->next;
  822. X    }
  823. X
  824. X  finalize_closure();
  825. X  free_storage();
  826. X}
  827. X
  828. X
  829. X
  830. Xint
  831. Xget_state(symbol)
  832. Xint symbol;
  833. X{
  834. X  register int key;
  835. X  register short *isp1;
  836. X  register short *isp2;
  837. X  register short *iend;
  838. X  register core *sp;
  839. X  register int found;
  840. X
  841. X  int n;
  842. X
  843. X#ifdef    TRACE
  844. X  fprintf(stderr, "Entering get_state, symbol = %d\n", symbol);
  845. X#endif
  846. X
  847. X  isp1 = kernel_base[symbol];
  848. X  iend = kernel_end[symbol];
  849. X  n = iend - isp1;
  850. X
  851. X  key = *isp1;
  852. X  assert(0 <= key && key < nitems);
  853. X  sp = state_table[key];
  854. X  if (sp)
  855. X    {
  856. X      found = 0;
  857. X      while (!found)
  858. X    {
  859. X      if (sp->nitems == n)
  860. X        {
  861. X          found = 1;
  862. X          isp1 = kernel_base[symbol];
  863. X          isp2 = sp->items;
  864. X
  865. X          while (found && isp1 < iend)
  866. X        {
  867. X          if (*isp1++ != *isp2++)
  868. X            found = 0;
  869. X        }
  870. X        }
  871. X
  872. X      if (!found)
  873. X        {
  874. X          if (sp->link)
  875. X        {
  876. X          sp = sp->link;
  877. X        }
  878. X          else
  879. X        {
  880. X          sp = sp->link = new_state(symbol);
  881. X          found = 1;
  882. X        }
  883. X        }
  884. X    }
  885. X    }
  886. X  else
  887. X    {
  888. X      state_table[key] = sp = new_state(symbol);
  889. X    }
  890. X
  891. X  return (sp->number);
  892. X}
  893. X
  894. X
  895. X
  896. Xinitialize_states()
  897. X{
  898. X    register int i;
  899. X    register short *start_derives;
  900. X    register core *p;
  901. X
  902. X    start_derives = derives[start_symbol];
  903. X    for (i = 0; start_derives[i] >= 0; ++i)
  904. X    continue;
  905. X
  906. X    p = (core *) MALLOC(sizeof(core) + i*sizeof(short));
  907. X    if (p == 0) no_space();
  908. X
  909. X    p->next = 0;
  910. X    p->link = 0;
  911. X    p->number = 0;
  912. X    p->accessing_symbol = 0;
  913. X    p->nitems = i;
  914. X
  915. X    for (i = 0;  start_derives[i] >= 0; ++i)
  916. X    p->items[i] = rrhs[start_derives[i]];
  917. X
  918. X    first_state = last_state = this_state = p;
  919. X    nstates = 1;
  920. X}
  921. X
  922. X
  923. Xnew_itemsets()
  924. X{
  925. X  register int i;
  926. X  register int shiftcount;
  927. X  register short *isp;
  928. X  register short *ksp;
  929. X  register int symbol;
  930. X
  931. X  for (i = 0; i < nsyms; i++)
  932. X    kernel_end[i] = 0;
  933. X
  934. X  shiftcount = 0;
  935. X  isp = itemset;
  936. X  while (isp < itemsetend)
  937. X    {
  938. X      i = *isp++;
  939. X      symbol = ritem[i];
  940. X      if (symbol > 0)
  941. X    {
  942. X          ksp = kernel_end[symbol];
  943. X
  944. X          if (!ksp)
  945. X        {
  946. X          shift_symbol[shiftcount++] = symbol;
  947. X          ksp = kernel_base[symbol];
  948. X        }
  949. X
  950. X          *ksp++ = i + 1;
  951. X          kernel_end[symbol] = ksp;
  952. X    }
  953. X    }
  954. X
  955. X  nshifts = shiftcount;
  956. X}
  957. X
  958. X
  959. X
  960. Xcore *
  961. Xnew_state(symbol)
  962. Xint symbol;
  963. X{
  964. X  register int n;
  965. X  register core *p;
  966. X  register short *isp1;
  967. X  register short *isp2;
  968. X  register short *iend;
  969. X
  970. X#ifdef    TRACE
  971. X  fprintf(stderr, "Entering new_state, symbol = %d\n", symbol);
  972. X#endif
  973. X
  974. X  if (nstates >= MAXSHORT)
  975. X    fatal("too many states");
  976. X
  977. X  isp1 = kernel_base[symbol];
  978. X  iend = kernel_end[symbol];
  979. X  n = iend - isp1;
  980. X
  981. X  p = (core *) allocate((unsigned) (sizeof(core) + (n - 1) * sizeof(short)));
  982. X  p->accessing_symbol = symbol;
  983. X  p->number = nstates;
  984. X  p->nitems = n;
  985. X
  986. X  isp2 = p->items;
  987. X  while (isp1 < iend)
  988. X    *isp2++ = *isp1++;
  989. X
  990. X  last_state->next = p;
  991. X  last_state = p;
  992. X
  993. X  nstates++;
  994. X
  995. X  return (p);
  996. X}
  997. X
  998. X
  999. X/* show_cores is used for debugging */
  1000. X
  1001. Xshow_cores()
  1002. X{
  1003. X    core *p;
  1004. X    int i, j, k, n;
  1005. X    int itemno;
  1006. X
  1007. X    k = 0;
  1008. X    for (p = first_state; p; ++k, p = p->next)
  1009. X    {
  1010. X    if (k) printf("\n");
  1011. X    printf("state %d, number = %d, accessing symbol = %s\n",
  1012. X        k, p->number, symbol_name[p->accessing_symbol]);
  1013. X    n = p->nitems;
  1014. X    for (i = 0; i < n; ++i)
  1015. X    {
  1016. X        itemno = p->items[i];
  1017. X        printf("%4d  ", itemno);
  1018. X        j = itemno;
  1019. X        while (ritem[j] >= 0) ++j;
  1020. X        printf("%s :", symbol_name[rlhs[-ritem[j]]]);
  1021. X        j = rrhs[-ritem[j]];
  1022. X        while (j < itemno)
  1023. X        printf(" %s", symbol_name[ritem[j++]]);
  1024. X        printf(" .");
  1025. X        while (ritem[j] >= 0)
  1026. X        printf(" %s", symbol_name[ritem[j++]]);
  1027. X        printf("\n");
  1028. X        fflush(stdout);
  1029. X    }
  1030. X    }
  1031. X}
  1032. X
  1033. X
  1034. X/* show_ritems is used for debugging */
  1035. X
  1036. Xshow_ritems()
  1037. X{
  1038. X    int i;
  1039. X
  1040. X    for (i = 0; i < nitems; ++i)
  1041. X    printf("ritem[%d] = %d\n", i, ritem[i]);
  1042. X}
  1043. X
  1044. X
  1045. X/* show_rrhs is used for debugging */
  1046. Xshow_rrhs()
  1047. X{
  1048. X    int i;
  1049. X
  1050. X    for (i = 0; i < nrules; ++i)
  1051. X    printf("rrhs[%d] = %d\n", i, rrhs[i]);
  1052. X}
  1053. X
  1054. X
  1055. X/* show_shifts is used for debugging */
  1056. X
  1057. Xshow_shifts()
  1058. X{
  1059. X    shifts *p;
  1060. X    int i, j, k;
  1061. X
  1062. X    k = 0;
  1063. X    for (p = first_shift; p; ++k, p = p->next)
  1064. X    {
  1065. X    if (k) printf("\n");
  1066. X    printf("shift %d, number = %d, nshifts = %d\n", k, p->number,
  1067. X        p->nshifts);
  1068. X    j = p->nshifts;
  1069. X    for (i = 0; i < j; ++i)
  1070. X        printf("\t%d\n", p->shift[i]);
  1071. X    }
  1072. X}
  1073. X
  1074. X
  1075. Xsave_shifts()
  1076. X{
  1077. X  register shifts *p;
  1078. X  register short *sp1;
  1079. X  register short *sp2;
  1080. X  register short *send;
  1081. X
  1082. X  p = (shifts *) allocate((unsigned) (sizeof(shifts) +
  1083. X            (nshifts - 1) * sizeof(short)));
  1084. X
  1085. X  p->number = this_state->number;
  1086. X  p->nshifts = nshifts;
  1087. X
  1088. X  sp1 = shiftset;
  1089. X  sp2 = p->shift;
  1090. X  send = shiftset + nshifts;
  1091. X
  1092. X  while (sp1 < send)
  1093. X    *sp2++ = *sp1++;
  1094. X
  1095. X  if (last_shift)
  1096. X    {
  1097. X      last_shift->next = p;
  1098. X      last_shift = p;
  1099. X    }
  1100. X  else
  1101. X    {
  1102. X      first_shift = p;
  1103. X      last_shift = p;
  1104. X    }
  1105. X}
  1106. X
  1107. X
  1108. X
  1109. Xsave_reductions()
  1110. X{
  1111. X  register short *isp;
  1112. X  register short *rp1;
  1113. X  register short *rp2;
  1114. X  register int item;
  1115. X  register int count;
  1116. X  register reductions *p;
  1117. X
  1118. X  short *rend;
  1119. X
  1120. X  count = 0;
  1121. X  for (isp = itemset; isp < itemsetend; isp++)
  1122. X    {
  1123. X      item = ritem[*isp];
  1124. X      if (item < 0)
  1125. X    {
  1126. X      redset[count++] = -item;
  1127. X    }
  1128. X    }
  1129. X
  1130. X  if (count)
  1131. X    {
  1132. X      p = (reductions *) allocate((unsigned) (sizeof(reductions) +
  1133. X                    (count - 1) * sizeof(short)));
  1134. X
  1135. X      p->number = this_state->number;
  1136. X      p->nreds = count;
  1137. X
  1138. X      rp1 = redset;
  1139. X      rp2 = p->rules;
  1140. X      rend = rp1 + count;
  1141. X
  1142. X      while (rp1 < rend)
  1143. X    *rp2++ = *rp1++;
  1144. X
  1145. X      if (last_reduction)
  1146. X    {
  1147. X      last_reduction->next = p;
  1148. X      last_reduction = p;
  1149. X    }
  1150. X      else
  1151. X    {
  1152. X      first_reduction = p;
  1153. X      last_reduction = p;
  1154. X    }
  1155. X    }
  1156. X}
  1157. X
  1158. X
  1159. Xset_derives()
  1160. X{
  1161. X  register int i, k;
  1162. X  register int lhs;
  1163. X  register short *rules;
  1164. X
  1165. X  derives = NEW2(nsyms, short *);
  1166. X  rules = NEW2(nvars + nrules, short);
  1167. X
  1168. X  k = 0;
  1169. X  for (lhs = start_symbol; lhs < nsyms; lhs++)
  1170. X    {
  1171. X      derives[lhs] = rules + k;
  1172. X      for (i = 0; i < nrules; i++)
  1173. X    {
  1174. X      if (rlhs[i] == lhs)
  1175. X        {
  1176. X          rules[k] = i;
  1177. X          k++;
  1178. X        }
  1179. X    }
  1180. X      rules[k] = -1;
  1181. X      k++;
  1182. X    }
  1183. X
  1184. X#ifdef    DEBUG
  1185. X  print_derives();
  1186. X#endif
  1187. X}
  1188. X
  1189. Xfree_derives()
  1190. X{
  1191. X  FREE(derives[start_symbol]);
  1192. X  FREE(derives);
  1193. X}
  1194. X
  1195. X#ifdef    DEBUG
  1196. Xprint_derives()
  1197. X{
  1198. X  register int i;
  1199. X  register short *sp;
  1200. X
  1201. X  printf("\nDERIVES\n\n");
  1202. X
  1203. X  for (i = start_symbol; i < nsyms; i++)
  1204. X    {
  1205. X      printf("%s derives ", symbol_name[i]);
  1206. X      for (sp = derives[i]; *sp >= 0; sp++)
  1207. X    {
  1208. X      printf("  %d", *sp);
  1209. X    }
  1210. X      putchar('\n');
  1211. X    }
  1212. X
  1213. X  putchar('\n');
  1214. X}
  1215. X#endif
  1216. X
  1217. X
  1218. Xset_nullable()
  1219. X{
  1220. X    register int i, j;
  1221. X    register int empty;
  1222. X    int done;
  1223. X
  1224. X    nullable = MALLOC(nsyms);
  1225. X    if (nullable == 0) no_space();
  1226. X
  1227. X    for (i = 0; i < nsyms; ++i)
  1228. X    nullable[i] = 0;
  1229. X
  1230. X    done = 0;
  1231. X    while (!done)
  1232. X    {
  1233. X    done = 1;
  1234. X    for (i = 1; i < nitems; i++)
  1235. X    {
  1236. X        empty = 1;
  1237. X        while ((j = ritem[i]) >= 0)
  1238. X        {
  1239. X        if (!nullable[j])
  1240. X            empty = 0;
  1241. X        ++i;
  1242. X        }
  1243. X        if (empty)
  1244. X        {
  1245. X        j = rlhs[-j];
  1246. X        if (!nullable[j])
  1247. X        {
  1248. X            nullable[j] = 1;
  1249. X            done = 0;
  1250. X        }
  1251. X        }
  1252. X    }
  1253. X    }
  1254. X
  1255. X#ifdef DEBUG
  1256. X    for (i = 0; i < nsyms; i++)
  1257. X    {
  1258. X    if (nullable[i])
  1259. X        printf("%s is nullable\n", symbol_name[i]);
  1260. X    else
  1261. X        printf("%s is not nullable\n", symbol_name[i]);
  1262. X    }
  1263. X#endif
  1264. X}
  1265. X
  1266. X
  1267. Xfree_nullable()
  1268. X{
  1269. X  FREE(nullable);
  1270. X}
  1271. X
  1272. X
  1273. Xlr0()
  1274. X{
  1275. X    set_derives();
  1276. X    set_nullable();
  1277. X    generate_states();
  1278. X}
  1279. END_OF_FILE
  1280. if test 9615 -ne `wc -c <'lr0.c'`; then
  1281.     echo shar: \"'lr0.c'\" unpacked with wrong size!
  1282. fi
  1283. # end of 'lr0.c'
  1284. fi
  1285. if test -f 'mkpar.c' -a "${1}" != "-c" ; then 
  1286.   echo shar: Will not clobber existing file \"'mkpar.c'\"
  1287. else
  1288. echo shar: Extracting \"'mkpar.c'\" \(6766 characters\)
  1289. sed "s/^X//" >'mkpar.c' <<'END_OF_FILE'
  1290. X#include "defs.h"
  1291. X
  1292. Xaction **parser;
  1293. Xint SRtotal;
  1294. Xint RRtotal;
  1295. Xshort *SRconflicts;
  1296. Xshort *RRconflicts;
  1297. Xshort *defred;
  1298. Xshort *rules_used;
  1299. Xshort nunused;
  1300. Xshort final_state;
  1301. X
  1302. Xstatic int SRcount;
  1303. Xstatic int RRcount;
  1304. X
  1305. Xextern action *parse_actions();
  1306. Xextern action *get_shifts();
  1307. Xextern action *add_reductions();
  1308. Xextern action *add_reduce();
  1309. X
  1310. X
  1311. Xmake_parser()
  1312. X{
  1313. X    register int i;
  1314. X
  1315. X    parser = NEW2(nstates, action *);
  1316. X    for (i = 0; i < nstates; i++)
  1317. X    parser[i] = parse_actions(i);
  1318. X
  1319. X    find_final_state();
  1320. X    remove_conflicts();
  1321. X    unused_rules();
  1322. X    if (SRtotal + RRtotal > 0) total_conflicts();
  1323. X    defreds();
  1324. X}
  1325. X
  1326. X
  1327. Xaction *
  1328. Xparse_actions(stateno)
  1329. Xregister int stateno;
  1330. X{
  1331. X    register action *actions;
  1332. X
  1333. X    actions = get_shifts(stateno);
  1334. X    actions = add_reductions(stateno, actions);
  1335. X    return (actions);
  1336. X}
  1337. X
  1338. X
  1339. Xaction *
  1340. Xget_shifts(stateno)
  1341. Xint stateno;
  1342. X{
  1343. X    register action *actions, *temp;
  1344. X    register shifts *sp;
  1345. X    register short *to_state;
  1346. X    register int i, k;
  1347. X    register int symbol;
  1348. X
  1349. X    actions = 0;
  1350. X    sp = shift_table[stateno];
  1351. X    if (sp)
  1352. X    {
  1353. X    to_state = sp->shift;
  1354. X    for (i = sp->nshifts - 1; i >= 0; i--)
  1355. X    {
  1356. X        k = to_state[i];
  1357. X        symbol = accessing_symbol[k];
  1358. X        if (ISTOKEN(symbol))
  1359. X        {
  1360. X        temp = NEW(action);
  1361. X        temp->next = actions;
  1362. X        temp->symbol = symbol;
  1363. X        temp->number = k;
  1364. X        temp->prec = symbol_prec[symbol];
  1365. X        temp->action_code = SHIFT;
  1366. X        temp->assoc = symbol_assoc[symbol];
  1367. X        actions = temp;
  1368. X        }
  1369. X    }
  1370. X    }
  1371. X    return (actions);
  1372. X}
  1373. X
  1374. Xaction *
  1375. Xadd_reductions(stateno, actions)
  1376. Xint stateno;
  1377. Xregister action *actions;
  1378. X{
  1379. X    register int i, j, m, n;
  1380. X    register int ruleno, tokensetsize;
  1381. X    register unsigned *rowp;
  1382. X
  1383. X    tokensetsize = WORDSIZE(ntokens);
  1384. X    m = lookaheads[stateno];
  1385. X    n = lookaheads[stateno + 1];
  1386. X    for (i = m; i < n; i++)
  1387. X    {
  1388. X    ruleno = LAruleno[i];
  1389. X    rowp = LA + i * tokensetsize;
  1390. X    for (j = ntokens - 1; j >= 0; j--)
  1391. X    {
  1392. X        if (BIT(rowp, j))
  1393. X        actions = add_reduce(actions, ruleno, j);
  1394. X    }
  1395. X    }
  1396. X    return (actions);
  1397. X}
  1398. X
  1399. X
  1400. Xaction *
  1401. Xadd_reduce(actions, ruleno, symbol)
  1402. Xregister action *actions;
  1403. Xregister int ruleno, symbol;
  1404. X{
  1405. X    register action *temp, *prev, *next;
  1406. X
  1407. X    prev = 0;
  1408. X    for (next = actions; next && next->symbol < symbol; next = next->next)
  1409. X    prev = next;
  1410. X
  1411. X    while (next && next->symbol == symbol && next->action_code == SHIFT)
  1412. X    {
  1413. X    prev = next;
  1414. X    next = next->next;
  1415. X    }
  1416. X
  1417. X    while (next && next->symbol == symbol &&
  1418. X        next->action_code == REDUCE && next->number < ruleno)
  1419. X    {
  1420. X    prev = next;
  1421. X    next = next->next;
  1422. X    }
  1423. X
  1424. X    temp = NEW(action);
  1425. X    temp->next = next;
  1426. X    temp->symbol = symbol;
  1427. X    temp->number = ruleno;
  1428. X    temp->prec = rprec[ruleno];
  1429. X    temp->action_code = REDUCE;
  1430. X    temp->assoc = rassoc[ruleno];
  1431. X
  1432. X    if (prev)
  1433. X    prev->next = temp;
  1434. X    else
  1435. X    actions = temp;
  1436. X
  1437. X    return (actions);
  1438. X}
  1439. X
  1440. X
  1441. Xfind_final_state()
  1442. X{
  1443. X    register int goal, i;
  1444. X    register short *to_state;
  1445. X    register shifts *p;
  1446. X
  1447. X    p = shift_table[0];
  1448. X    to_state = p->shift;
  1449. X    goal = ritem[1];
  1450. X    for (i = p->nshifts - 1; i >= 0; --i)
  1451. X    {
  1452. X    final_state = to_state[i];
  1453. X    if (accessing_symbol[final_state] == goal) break;
  1454. X    }
  1455. X}
  1456. X
  1457. X
  1458. Xunused_rules()
  1459. X{
  1460. X    register int i;
  1461. X    register action *p;
  1462. X
  1463. X    rules_used = (short *) MALLOC(nrules*sizeof(short));
  1464. X    if (rules_used == 0) no_space();
  1465. X
  1466. X    for (i = 0; i < nrules; ++i)
  1467. X    rules_used[i] = 0;
  1468. X
  1469. X    for (i = 0; i < nstates; ++i)
  1470. X    {
  1471. X    for (p = parser[i]; p; p = p->next)
  1472. X    {
  1473. X        if (p->action_code == REDUCE && p->suppressed == 0)
  1474. X        rules_used[p->number] = 1;
  1475. X    }
  1476. X    }
  1477. X
  1478. X    nunused = 0;
  1479. X    for (i = 3; i < nrules; ++i)
  1480. X    if (!rules_used[i]) ++nunused;
  1481. X
  1482. X    if (nunused)
  1483. X    if (nunused == 1)
  1484. X        fprintf(stderr, "%s: 1 rule never reduced\n", myname);
  1485. X    else
  1486. X        fprintf(stderr, "%s: %d rules never reduced\n", myname, nunused);
  1487. X}
  1488. X
  1489. X
  1490. Xremove_conflicts()
  1491. X{
  1492. X    register int i;
  1493. X    register int symbol;
  1494. X    register action *p, *q;
  1495. X
  1496. X    SRtotal = 0;
  1497. X    RRtotal = 0;
  1498. X    SRconflicts = NEW2(nstates, short);
  1499. X    RRconflicts = NEW2(nstates, short);
  1500. X    for (i = 0; i < nstates; i++)
  1501. X    {
  1502. X    SRcount = 0;
  1503. X    RRcount = 0;
  1504. X    for (p = parser[i]; p; p = q->next)
  1505. X    {
  1506. X        symbol = p->symbol;
  1507. X        q = p;
  1508. X        while (q->next && q->next->symbol == symbol)
  1509. X        q = q->next;
  1510. X        if (i == final_state && symbol == 0)
  1511. X        end_conflicts(p, q);
  1512. X        else if (p != q)
  1513. X        resolve_conflicts(p, q);
  1514. X    }
  1515. X    SRtotal += SRcount;
  1516. X    RRtotal += RRcount;
  1517. X    SRconflicts[i] = SRcount;
  1518. X    RRconflicts[i] = RRcount;
  1519. X    }
  1520. X}
  1521. X
  1522. X
  1523. Xend_conflicts(p, q)
  1524. Xregister action *p, *q;
  1525. X{
  1526. X    for (;;)
  1527. X    {
  1528. X    SRcount++;
  1529. X    p->suppressed = 1;
  1530. X    if (p == q) break;
  1531. X    p = p->next;
  1532. X    }
  1533. X}
  1534. X
  1535. X
  1536. Xresolve_conflicts(first, last)
  1537. Xregister action *first, *last;
  1538. X{
  1539. X    register action *p;
  1540. X    register int count;
  1541. X
  1542. X    count = 1;
  1543. X    for (p = first; p != last; p = p->next)
  1544. X     ++count;
  1545. X    assert(count > 1);
  1546. X
  1547. X    if (first->action_code == SHIFT && count == 2 &&
  1548. X        first->prec > 0 && last->prec > 0)
  1549. X    {
  1550. X    if (first->prec == last->prec)
  1551. X    {
  1552. X        if (first->assoc == LEFT)
  1553. X        first->suppressed = 2;
  1554. X        else if (first->assoc == RIGHT)
  1555. X        last->suppressed = 2;
  1556. X        else
  1557. X        {
  1558. X        first->suppressed = 2;
  1559. X        last->suppressed = 2;
  1560. X        first->action_code = ERROR;
  1561. X        last->action_code = ERROR;
  1562. X        }
  1563. X    }
  1564. X    else if (first->prec < last->prec)
  1565. X        first->suppressed = 2;
  1566. X    else
  1567. X        last->suppressed = 2;
  1568. X    }
  1569. X    else
  1570. X    {
  1571. X    if (first->action_code == SHIFT)
  1572. X        SRcount += (count - 1);
  1573. X        else
  1574. X        RRcount += (count - 1);
  1575. X    for (p = first; p != last; p = p->next, p->suppressed = 1)
  1576. X        continue;
  1577. X    }
  1578. X}
  1579. X
  1580. X
  1581. Xtotal_conflicts()
  1582. X{
  1583. X    fprintf(stderr, "%s: ", myname);
  1584. X    if (SRtotal == 1)
  1585. X    fprintf(stderr, "1 shift/reduce conflict");
  1586. X    else if (SRtotal > 1)
  1587. X    fprintf(stderr, "%d shift/reduce conflicts", SRtotal);
  1588. X
  1589. X    if (SRtotal && RRtotal)
  1590. X    fprintf(stderr, ", ");
  1591. X
  1592. X    if (RRtotal == 1)
  1593. X    fprintf(stderr, "1 reduce/reduce conflict");
  1594. X    else if (RRtotal > 1)
  1595. X    fprintf(stderr, "%d reduce/reduce conflicts", RRtotal);
  1596. X
  1597. X    fprintf(stderr, ".\n");
  1598. X}
  1599. X
  1600. X
  1601. Xint
  1602. Xsole_reduction(stateno)
  1603. Xint stateno;
  1604. X{
  1605. X    register int count, ruleno;
  1606. X    register action *p;
  1607. X
  1608. X    count = 0;
  1609. X    ruleno = 0; 
  1610. X    for (p = parser[stateno]; p; p = p->next)
  1611. X    {
  1612. X    if (p->action_code == SHIFT && p->suppressed == 0)
  1613. X        return (0);
  1614. X    else if (p->action_code == REDUCE && p->suppressed == 0)
  1615. X    {
  1616. X        if (ruleno > 0 && p->number != ruleno)
  1617. X        return (0);
  1618. X        if (p->symbol != 1)
  1619. X        ++count;
  1620. X        ruleno = p->number;
  1621. X    }
  1622. X    }
  1623. X
  1624. X    if (count == 0)
  1625. X    return (0);
  1626. X    return (ruleno);
  1627. X}
  1628. X
  1629. X
  1630. Xdefreds()
  1631. X{
  1632. X    register int i;
  1633. X
  1634. X    defred = NEW2(nstates, short);
  1635. X    for (i = 0; i < nstates; i++)
  1636. X    defred[i] = sole_reduction(i);
  1637. X}
  1638. Xfree_action_row(p)
  1639. Xregister action *p;
  1640. X{
  1641. X  register action *q;
  1642. X
  1643. X  while (p)
  1644. X    {
  1645. X      q = p->next;
  1646. X      FREE(p);
  1647. X      p = q;
  1648. X    }
  1649. X}
  1650. X
  1651. Xfree_parser()
  1652. X{
  1653. X  register int i;
  1654. X
  1655. X  for (i = 0; i < nstates; i++)
  1656. X    free_action_row(parser[i]);
  1657. X
  1658. X  FREE(parser);
  1659. X}
  1660. END_OF_FILE
  1661. if test 6766 -ne `wc -c <'mkpar.c'`; then
  1662.     echo shar: \"'mkpar.c'\" unpacked with wrong size!
  1663. fi
  1664. # end of 'mkpar.c'
  1665. fi
  1666. if test -f 'skeleton.c' -a "${1}" != "-c" ; then 
  1667.   echo shar: Will not clobber existing file \"'skeleton.c'\"
  1668. else
  1669. echo shar: Extracting \"'skeleton.c'\" \(7465 characters\)
  1670. sed "s/^X//" >'skeleton.c' <<'END_OF_FILE'
  1671. X#include "defs.h"
  1672. X
  1673. X/*  The three line banner used here should be replaced with a one line    */
  1674. X/*  #ident directive if the target C compiler supports #ident        */
  1675. X/*  directives.                                */
  1676. X/*                                    */
  1677. X/*  If the skeleton is changed, the banner should be changed so that    */
  1678. X/*  the altered version can easily be distinguished from the original.    */
  1679. X
  1680. Xchar *banner[] =
  1681. X{
  1682. X    "#ifndef lint",
  1683. X    "char yysccsid[] = \"@(#)yaccpar    1.4 (Berkeley) 02/25/90\";",
  1684. X    "#endif",
  1685. X    0
  1686. X};
  1687. X
  1688. X
  1689. Xchar *header[] =
  1690. X{
  1691. X    "#define yyclearin (yychar=(-1))",
  1692. X    "#define yyerrok (yyerrflag=0)",
  1693. X    "#ifndef YYSTACKSIZE",
  1694. X    "#ifdef YYMAXDEPTH",
  1695. X    "#define YYSTACKSIZE YYMAXDEPTH",
  1696. X    "#else",
  1697. X    "#define YYSTACKSIZE 300",
  1698. X    "#endif",
  1699. X    "#endif",
  1700. X    "int yydebug;",
  1701. X    "int yynerrs;",
  1702. X    "int yyerrflag;",
  1703. X    "int yychar;",
  1704. X    "short *yyssp;",
  1705. X    "YYSTYPE *yyvsp;",
  1706. X    "YYSTYPE yyval;",
  1707. X    "YYSTYPE yylval;",
  1708. X    "#define yystacksize YYSTACKSIZE",
  1709. X    "short yyss[YYSTACKSIZE];",
  1710. X    "YYSTYPE yyvs[YYSTACKSIZE];",
  1711. X    0
  1712. X};
  1713. X
  1714. X
  1715. Xchar *body[] =
  1716. X{
  1717. X    "#define YYABORT goto yyabort",
  1718. X    "#define YYACCEPT goto yyaccept",
  1719. X    "#define YYERROR goto yyerrlab",
  1720. X    "int",
  1721. X    "yyparse()",
  1722. X    "{",
  1723. X    "    register int yym, yyn, yystate;",
  1724. X    "#if YYDEBUG",
  1725. X    "    register char *yys;",
  1726. X    "    extern char *getenv();",
  1727. X    "",
  1728. X    "    if (yys = getenv(\"YYDEBUG\"))",
  1729. X    "    {",
  1730. X    "        yyn = *yys;",
  1731. X    "        if (yyn >= '0' && yyn <= '9')",
  1732. X    "            yydebug = yyn - '0';",
  1733. X    "    }",
  1734. X    "#endif",
  1735. X    "",
  1736. X    "    yynerrs = 0;",
  1737. X    "    yyerrflag = 0;",
  1738. X    "    yychar = (-1);",
  1739. X    "",
  1740. X    "    yyssp = yyss;",
  1741. X    "    yyvsp = yyvs;",
  1742. X    "    *yyssp = yystate = 0;",
  1743. X    "",
  1744. X    "yyloop:",
  1745. X    "    if (yyn = yydefred[yystate]) goto yyreduce;",
  1746. X    "    if (yychar < 0)",
  1747. X    "    {",
  1748. X    "        if ((yychar = yylex()) < 0) yychar = 0;",
  1749. X    "#if YYDEBUG",
  1750. X    "        if (yydebug)",
  1751. X    "        {",
  1752. X    "            yys = 0;",
  1753. X    "            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
  1754. X    "            if (!yys) yys = \"illegal-symbol\";",
  1755. X    "            printf(\"yydebug: state %d, reading %d (%s)\\n\", yystate,",
  1756. X    "                    yychar, yys);",
  1757. X    "        }",
  1758. X    "#endif",
  1759. X    "    }",
  1760. X    "    if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&",
  1761. X    "            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
  1762. X    "    {",
  1763. X    "#if YYDEBUG",
  1764. X    "        if (yydebug)",
  1765. X    "            printf(\"yydebug: state %d, shifting to state %d\\n\",",
  1766. X    "                    yystate, yytable[yyn]);",
  1767. X    "#endif",
  1768. X    "        if (yyssp >= yyss + yystacksize - 1)",
  1769. X    "        {",
  1770. X    "            goto yyoverflow;",
  1771. X    "        }",
  1772. X    "        *++yyssp = yystate = yytable[yyn];",
  1773. X    "        *++yyvsp = yylval;",
  1774. X    "        yychar = (-1);",
  1775. X    "        if (yyerrflag > 0)  --yyerrflag;",
  1776. X    "        goto yyloop;",
  1777. X    "    }",
  1778. X    "    if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&",
  1779. X    "            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
  1780. X    "    {",
  1781. X    "        yyn = yytable[yyn];",
  1782. X    "        goto yyreduce;",
  1783. X    "    }",
  1784. X    "    if (yyerrflag) goto yyinrecovery;",
  1785. X    "#ifdef lint",
  1786. X    "    goto yynewerror;",
  1787. X    "#endif",
  1788. X    "yynewerror:",
  1789. X    "    yyerror(\"syntax error\");",
  1790. X    "#ifdef lint",
  1791. X    "    goto yyerrlab;",
  1792. X    "#endif",
  1793. X    "yyerrlab:",
  1794. X    "    ++yynerrs;",
  1795. X    "yyinrecovery:",
  1796. X    "    if (yyerrflag < 3)",
  1797. X    "    {",
  1798. X    "        yyerrflag = 3;",
  1799. X    "        for (;;)",
  1800. X    "        {",
  1801. X    "            if ((yyn = yysindex[*yyssp]) && (yyn += YYERRCODE) >= 0 &&",
  1802. X    "                    yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)",
  1803. X    "            {",
  1804. X    "#if YYDEBUG",
  1805. X    "                if (yydebug)",
  1806. X    "                    printf(\"yydebug: state %d, error recovery shifting\\",
  1807. X    " to state %d\\n\", *yyssp, yytable[yyn]);",
  1808. X    "#endif",
  1809. X    "                if (yyssp >= yyss + yystacksize - 1)",
  1810. X    "                {",
  1811. X    "                    goto yyoverflow;",
  1812. X    "                }",
  1813. X    "                *++yyssp = yystate = yytable[yyn];",
  1814. X    "                *++yyvsp = yylval;",
  1815. X    "                goto yyloop;",
  1816. X    "            }",
  1817. X    "            else",
  1818. X    "            {",
  1819. X    "#if YYDEBUG",
  1820. X    "                if (yydebug)",
  1821. X    "                    printf(\"yydebug: error recovery discarding state %d\
  1822. X\\n\",",
  1823. X    "                            *yyssp);",
  1824. X    "#endif",
  1825. X    "                if (yyssp <= yyss) goto yyabort;",
  1826. X    "                --yyssp;",
  1827. X    "                --yyvsp;",
  1828. X    "            }",
  1829. X    "        }",
  1830. X    "    }",
  1831. X    "    else",
  1832. X    "    {",
  1833. X    "        if (yychar == 0) goto yyabort;",
  1834. X    "#if YYDEBUG",
  1835. X    "        if (yydebug)",
  1836. X    "        {",
  1837. X    "            yys = 0;",
  1838. X    "            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
  1839. X    "            if (!yys) yys = \"illegal-symbol\";",
  1840. X    "            printf(\"yydebug: state %d, error recovery discards token %d\
  1841. X (%s)\\n\",",
  1842. X    "                    yystate, yychar, yys);",
  1843. X    "        }",
  1844. X    "#endif",
  1845. X    "        yychar = (-1);",
  1846. X    "        goto yyloop;",
  1847. X    "    }",
  1848. X    "yyreduce:",
  1849. X    "#if YYDEBUG",
  1850. X    "    if (yydebug)",
  1851. X    "        printf(\"yydebug: state %d, reducing by rule %d (%s)\\n\",",
  1852. X    "                yystate, yyn, yyrule[yyn]);",
  1853. X    "#endif",
  1854. X    "    yym = yylen[yyn];",
  1855. X    "    yyval = yyvsp[1-yym];",
  1856. X    "    switch (yyn)",
  1857. X    "    {",
  1858. X    0
  1859. X};
  1860. X
  1861. X
  1862. Xchar *trailer[] =
  1863. X{
  1864. X    "    }",
  1865. X    "    yyssp -= yym;",
  1866. X    "    yystate = *yyssp;",
  1867. X    "    yyvsp -= yym;",
  1868. X    "    yym = yylhs[yyn];",
  1869. X    "    if (yystate == 0 && yym == 0)",
  1870. X    "    {",
  1871. X    "#ifdef YYDEBUG",
  1872. X    "        if (yydebug)",
  1873. X    "            printf(\"yydebug: after reduction, shifting from state 0 to\\",
  1874. X    " state %d\\n\", YYFINAL);",
  1875. X    "#endif",
  1876. X    "        yystate = YYFINAL;",
  1877. X    "        *++yyssp = YYFINAL;",
  1878. X    "        *++yyvsp = yyval;",
  1879. X    "        if (yychar < 0)",
  1880. X    "        {",
  1881. X    "            if ((yychar = yylex()) < 0) yychar = 0;",
  1882. X    "#if YYDEBUG",
  1883. X    "            if (yydebug)",
  1884. X    "            {",
  1885. X    "                yys = 0;",
  1886. X    "                if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
  1887. X    "                if (!yys) yys = \"illegal-symbol\";",
  1888. X    "                printf(\"yydebug: state %d, reading %d (%s)\\n\",",
  1889. X    "                        YYFINAL, yychar, yys);",
  1890. X    "            }",
  1891. X    "#endif",
  1892. X    "        }",
  1893. X    "        if (yychar == 0) goto yyaccept;",
  1894. X    "        goto yyloop;",
  1895. X    "    }",
  1896. X    "    if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&",
  1897. X    "            yyn <= YYTABLESIZE && yycheck[yyn] == yystate)",
  1898. X    "        yystate = yytable[yyn];",
  1899. X    "    else",
  1900. X    "        yystate = yydgoto[yym];",
  1901. X    "#ifdef YYDEBUG",
  1902. X    "    if (yydebug)",
  1903. X    "        printf(\"yydebug: after reduction, shifting from state %d \\",
  1904. X    "to state %d\\n\", *yyssp, yystate);",
  1905. X    "#endif",
  1906. X    "    if (yyssp >= yyss + yystacksize - 1)",
  1907. X    "    {",
  1908. X    "        goto yyoverflow;",
  1909. X    "    }",
  1910. X    "    *++yyssp = yystate;",
  1911. X    "    *++yyvsp = yyval;",
  1912. X    "    goto yyloop;",
  1913. X    "yyoverflow:",
  1914. X    "    yyerror(\"yacc stack overflow\");",
  1915. X    "yyabort:",
  1916. X    "    return (1);",
  1917. X    "yyaccept:",
  1918. X    "    return (0);",
  1919. X    "}",
  1920. X    0
  1921. X};
  1922. X
  1923. X
  1924. Xwrite_section(section)
  1925. Xchar *section[];
  1926. X{
  1927. X    register int i;
  1928. X
  1929. X    for (i = 0; section[i]; ++i)
  1930. X    {
  1931. X    ++outline;
  1932. X    fprintf(output_file, "%s\n", section[i]);
  1933. X    }
  1934. X}
  1935. END_OF_FILE
  1936. if test 7465 -ne `wc -c <'skeleton.c'`; then
  1937.     echo shar: \"'skeleton.c'\" unpacked with wrong size!
  1938. fi
  1939. # end of 'skeleton.c'
  1940. fi
  1941. echo shar: End of archive 2 \(of 5\).
  1942. cp /dev/null ark2isdone
  1943. MISSING=""
  1944. for I in 1 2 3 4 5 ; do
  1945.     if test ! -f ark${I}isdone ; then
  1946.     MISSING="${MISSING} ${I}"
  1947.     fi
  1948. done
  1949. if test "${MISSING}" = "" ; then
  1950.     echo You have unpacked all 5 archives.
  1951.     rm -f ark[1-9]isdone
  1952. else
  1953.     echo You still need to unpack the following archives:
  1954.     echo "        " ${MISSING}
  1955. fi
  1956. ##  End of shell archive.
  1957. exit 0
  1958.